//
//  GBNoticeTimeView.m
//  PublicSentimentMonitorSystem
//
//  Created by 郭斌 on 15/9/8.
//  Copyright (c) 2015年 魏强. All rights reserved.
//

#import "GBNoticeTimeView.h"
#import "AFNetworking.h"
#import "AFHTTPRequestOperation.h"

@interface GBNoticeTimeView()<UIPickerViewDataSource,UIPickerViewDelegate>
{
    // 9.8
    UIView *selectView; //选择view
    UIButton *allDayBtn; //全天按钮
    UIButton *diyBtn; //自定义按钮
    UIButton *cancelBtn; //取消按钮
    UIButton *sureBtn; //确定按钮
    UIView *selectView2; //自定义时间界面的选择view
    UIButton *cancelBtn2; //自定义时间界面的取消按钮
    UIButton *sureBtn2; //自定义时间界面的确定按钮
    NSArray *timeArr; //时间数组
    UIPickerView *datePicker1; //选择时间
    UIPickerView *datePicker2;
    NSString *data1; //第一个时间
    NSString *data2; //第二个时间
    NSString *DoItYouselfUrl; //发送的Url;
    
}

@end

@implementation GBNoticeTimeView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.35];
        timeArr = [[NSArray alloc] initWithObjects:@"0",@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10",@"11",@"12",@"13",@"14",@"15",@"16",@"17",@"18",@"19",@"20",@"21",@"22",@"23", nil];
        //创建control
        [self createControl];
        //创建选择view
        [self createTheSelectView];
    }
    
    return self;
}

#pragma mark - 创建选择view
-(void)createTheSelectView
{
    selectView = [[UIView alloc] initWithFrame:CGRectMake(screenW / 2.0 - 120, 150, 240, 250)];
    selectView.backgroundColor = [UIColor whiteColor];
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 40)];
    label.text = @"通知时间";
    label.textColor = WQColor(106, 107, 108);
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = WQColor(206, 208, 209);
    [selectView addSubview:label];
    
    allDayBtn = [UIButton buttonWithType:0];
    allDayBtn.frame = CGRectMake(80, 50, 80, 40);
    [allDayBtn setTitle:@"全天" forState:UIControlStateNormal];
    allDayBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [allDayBtn setTitleColor:WQColor(190, 208, 209) forState:UIControlStateNormal];
    [allDayBtn setTitleColor:WQColor(100, 188, 126) forState:UIControlStateSelected];
    allDayBtn.selected = YES;
    [allDayBtn addTarget:self action:@selector(AllDayClick) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:allDayBtn];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(85, 94.5, 70, 1)];
    line.backgroundColor = WQColor(237, 238, 240);
    [selectView addSubview:line];
    
    diyBtn = [UIButton buttonWithType:0];
    diyBtn.frame = CGRectMake(80, 100, 80, 40);
    [diyBtn setTitle:@"自定义时间" forState:UIControlStateNormal];
    diyBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [diyBtn setTitleColor:WQColor(190, 208, 209) forState:UIControlStateNormal];
    [diyBtn setTitleColor:WQColor(100, 188, 126) forState:UIControlStateSelected];
    [diyBtn addTarget:self action:@selector(DIYClick) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:diyBtn];
    
    cancelBtn = [UIButton buttonWithType:0];
    cancelBtn.frame = CGRectMake(50, 200, 50, 40);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelBtn setTitleColor:WQColor(106, 107, 108) forState:UIControlStateNormal];
    [cancelBtn addTarget:self action:@selector(disappear) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:cancelBtn];
    
    sureBtn = [UIButton buttonWithType:0];
    sureBtn.frame = CGRectMake(150, 200, 50, 40);
    [sureBtn setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    [sureBtn setTitleColor:WQColor(106, 107, 108) forState:UIControlStateNormal];
    [sureBtn addTarget:self action:@selector(SureSelect) forControlEvents:UIControlEventTouchUpInside];
    [selectView addSubview:sureBtn];
    
    [self addSubview:selectView];
}

#pragma mark - 确定选择
-(void)SureSelect
{
    int d1 = [data1 intValue];
    int d2 = [data2 intValue];
    
    if(data1 != NULL)
    {
        if(d1 >= d2)
        {
            UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"开始时间不能大于等于截止时间，请重新选择！" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [NSTimer scheduledTimerWithTimeInterval:1.50f
                                            target:self
                                            selector:@selector(timerFireMethod:)
                                            userInfo:promptAlert
                                            repeats:YES];
            [promptAlert show];
            return;
        }
    }
    
    KDAccount *account = [KDAccountTool account];
    //读取数据
    //全部时段
    if(data1 == NULL)
    {
        DoItYouselfUrl = [NSString stringWithFormat:@"%@&method=updateNoticeTimeSpace&token=%@%@%d%@%d",ServerActionAddr,account.tokens,@"&begTime=",0,@"&endTime=",23];
    }
    else
    {
        DoItYouselfUrl = [NSString stringWithFormat:@"%@&method=updateNoticeTimeSpace&token=%@%@%d%@%d",ServerActionAddr,account.tokens,@"&begTime=",d1,@"&endTime=",d2];
    }
    //NSLog(@"%@",DoItYouselfUrl);
    
    NSURLRequest *httpReq = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:DoItYouselfUrl]];
    AFHTTPRequestOperation *reqOper = [[AFHTTPRequestOperation alloc] initWithRequest:httpReq];
    [reqOper setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *op,id success){
        NSHTTPURLResponse *response = [op response];
        NSInteger code = [response statusCode];
        if (code == 200)
        {
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:[op responseData] options:NSJSONReadingMutableContainers error:nil];
            if([jsonDict[@"status"] isEqualToString:@"ok"])
            {
                UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"设置成功" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [NSTimer scheduledTimerWithTimeInterval:1.50f
                                                 target:self
                                               selector:@selector(timerFireMethod:)
                                               userInfo:promptAlert
                                                repeats:YES];
                [promptAlert show];
                //请求成功当前界面消失
                [self disappear];
                
                if(data1 == NULL)
                {
                    data1 = @"100";
                    data2 = @"100";
                }
                NSArray *arrayData = @[data1, data2];
                [self.delegate selectTheResult:arrayData];
            }
            else
            {
                UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"设置失败" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
                [NSTimer scheduledTimerWithTimeInterval:1.50f
                                                 target:self
                                               selector:@selector(timerFireMethod:)
                                               userInfo:promptAlert
                                                repeats:YES];
                [promptAlert show];
            }
        }
    } failure:^(AFHTTPRequestOperation *op, NSError *error) {
        if (error) {
            //NSLog(@"req error %@",error);
            UIAlertView *promptAlert = [[UIAlertView alloc] initWithTitle:@"设置失败" message:nil delegate:nil cancelButtonTitle:nil otherButtonTitles:nil];
            [NSTimer scheduledTimerWithTimeInterval:1.50f
                                             target:self
                                           selector:@selector(timerFireMethod:)
                                           userInfo:promptAlert
                                            repeats:YES];
            [promptAlert show];
        }
    }];
    [reqOper start];
    //NSLog(@"data1 = %@, data2 = %@", data1, data2);
}

#pragma mark - 弹出的对话框内容
- (void)timerFireMethod:(NSTimer*)theTimer//弹出框
{
    UIAlertView *promptAlert = (UIAlertView*)[theTimer userInfo];
    [promptAlert dismissWithClickedButtonIndex:0 animated:NO];
    promptAlert = nil;
}

#pragma mark - 自定义按钮
-(void)DIYClick
{
    if(diyBtn.selected == YES)
    {
        diyBtn.selected = YES;
        allDayBtn.selected = NO;
    }
    else
    {
        diyBtn.selected = NO;
        allDayBtn.selected = YES;
    }
    //创建自定义时间界面
    [UIView animateWithDuration:0.5 animations:^{
        [self createTheSelectView2];
    }];
}

#pragma mark - 创建自定义时间界面
-(void)createTheSelectView2
{
    selectView2 = [[UIView alloc] initWithFrame:CGRectMake(screenW / 2.0 - 120, 100, 240, 350)];
    selectView2.backgroundColor = [UIColor whiteColor];
    
    data1 = @"0";
    data2 = @"0";
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 240, 40)];
    label.text = @"通知时间";
    label.textColor = WQColor(106, 107, 108);
    label.textAlignment = NSTextAlignmentCenter;
    label.backgroundColor = WQColor(206, 208, 209);
    [selectView2 addSubview:label];
    
    UILabel *llabell = [[UILabel alloc] initWithFrame:CGRectMake(80, 50, 80, 40)];
    llabell.text = @"自定义时间";
    llabell.font = [UIFont systemFontOfSize:15];
    llabell.textColor = WQColor(100, 188, 126);
    [selectView2 addSubview:llabell];
    
    UILabel *line = [[UILabel alloc] initWithFrame:CGRectMake(80, 94.5, 80, 1)];
    line.backgroundColor = WQColor(237, 238, 240);
    [selectView2 addSubview:line];
    
    datePicker1 = [[UIPickerView alloc] initWithFrame:CGRectMake(77, 96, 40, 100)];
    datePicker1.showsSelectionIndicator = YES;
    datePicker1.tag = 110;
    datePicker1.delegate = self;
    datePicker1.dataSource = self;
    [selectView2 addSubview:datePicker1];
    
    datePicker2 = [[UIPickerView alloc] initWithFrame:CGRectMake(120, 96, 40, 100)];
    datePicker2.showsSelectionIndicator = YES;
    datePicker2.tag = 111;
    datePicker2.delegate = self;
    datePicker2.dataSource = self;
    [selectView2 addSubview:datePicker2];
    
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(80, 254.5, 80, 1)];
    line2.backgroundColor = WQColor(237, 238, 240);
    [selectView2 addSubview:line2];
    
    cancelBtn2 = [UIButton buttonWithType:0];
    cancelBtn2.frame = CGRectMake(50, 300, 50, 40);
    [cancelBtn2 setTitle:@"取消" forState:UIControlStateNormal];
    cancelBtn2.titleLabel.font = [UIFont systemFontOfSize:15];
    [cancelBtn2 setTitleColor:WQColor(106, 107, 108) forState:UIControlStateNormal];
    [cancelBtn2 addTarget:self action:@selector(disappear) forControlEvents:UIControlEventTouchUpInside];
    [selectView2 addSubview:cancelBtn2];
    
    sureBtn2 = [UIButton buttonWithType:0];
    sureBtn2.frame = CGRectMake(150, 300, 50, 40);
    [sureBtn2 setTitle:@"确定" forState:UIControlStateNormal];
    sureBtn2.titleLabel.font = [UIFont systemFontOfSize:15];
    [sureBtn2 setTitleColor:WQColor(106, 107, 108) forState:UIControlStateNormal];
    [sureBtn2 addTarget:self action:@selector(SureSelect) forControlEvents:UIControlEventTouchUpInside];
    [selectView2 addSubview:sureBtn2];
    
    [self addSubview:selectView2];
}

#pragma mark - 全天按钮
-(void)AllDayClick
{
    //NSLog(@"全天");
    if(allDayBtn.selected == YES)
    {
        allDayBtn.selected = YES;
        diyBtn.selected = NO;
    }
    else
    {
        allDayBtn.selected = NO;
        diyBtn.selected = YES;
    }
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    if(pickerView.tag == 110)
    {
        data1 = [NSString stringWithFormat:@"%@",[timeArr objectAtIndex:row]];
    }
    else
    {
        data2 = [NSString stringWithFormat:@"%@",[timeArr objectAtIndex:row]];
    }
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return (1);
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return [timeArr count];
}

- (NSString *)pickerView:(UIPickerView *)pickerView
             titleForRow:(NSInteger)row
            forComponent:(NSInteger)component
{
    return [timeArr objectAtIndex:row];
}

#pragma mark - 点击任意地方自身消失
-(void)createControl
{
    UIControl *control1 = [[UIControl alloc] initWithFrame:CGRectMake(0, 0, screenW, screenH)];
    control1.backgroundColor = [UIColor clearColor];
    [control1 addTarget:self action:@selector(disappear) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:control1];
}

#pragma mark - 当前整个界面消失
-(void)disappear
{
    [UIView animateWithDuration:0.5 animations:^{
        [self removeFromSuperview];
    }];
}

- (void)dealloc
{
    [self removeFromSuperview];
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
